<?php
/**
 * 
 * 日期、时间工具类
 * @author Administrator
 *
 */
class DateUtils{
	/**
	 * 
	 * 返回当前时间
	 * @return string
	 */
	public function getCurrentTime(){
		return gmdate("Y-m-d H:i:s", time() + 3600 * 8);
	}
	/**
	 * 
	 * 获得时间戳(10位数字，以秒为单位)
	 * @param string $timeStr 日期时间字符串
	 * @return string
	 */
	public function getTimestamp($timeStr){
		return @strtotime($timeStr);
	}
	/**
	 * 
	 * 通过时间戳获得时间格式字符串
	 * @param string $timeStamp
	 */
	public function getTimeByTimestamp($timeStamp){
		return @date('Y-m-d H:i:s',$timeStamp);
	}
	
	/**
	 * 
	 * 获得两个时间值的差(单位为秒)
	 * 注意:
	 * 1、如果为负值，表示$timeStr2比$timeStr1小
	 * @param string $timeStr1 
	 * @param string $timeStr2
	 * @return string
	 */
	public function getTimeBetween($timeStr1,$timeStr2){
		//获得时间戳
		$timestr1Timestamp=@strtotime($timeStr1);
		$timestr2Timestamp=@strtotime($timeStr2);
		//时间差(秒)
		$timestampBetween=$timestr2Timestamp-$timestr1Timestamp;
		return $timestampBetween;
	}
	/**
	 * 
	 * 获得时间的格式化形式(XX天XX小时XX分钟XX秒)
	 * @param string $secondStr
	 */
	public function getFormateTime($secondCount){
		//天
		$unitDay=24*60*60;
		//小时
		$unitHour=60*60;
		$unitMinute=60;
		
		$str="";
		
		//取整
		$intDay=$secondCount/$unitDay;
		$intHour=0;
		$intMinue=0;
		
		//取余数
		$residueDay=0;
		$residueHour=0;
		$residueMinue=0;
		//时间单位值
		$day=0;
		$hour=0;
		$minute=0;
		$second=0;
		if($intDay==0){
			//global $unitDay,$unitHour,$unitMinute,$intDay,$intHour,$intMinue; 
			$intHour=$secondCount/$unitHour;
			if($intHour==0){
				$residueHour=$secondCount;
				$intMinue=$residueHour/$unitMinute;
				if($intMinue==0){
					$residueMinue=$residueHour;
					$second=$residueMinue;
				}else{
					$minute=$intMinue;
					$residueMinue=$residueHour%$unitMinute;
					$second=$residueMinue;
				}
			}else{
				$hour=$intHour;//获得小时值
				$residueHour=$secondCount%unitHour;//
				$intMinue=$residueHour/$unitMinute;
				if($intMinue==0){
					$residueMinue=$residueHour;
					$second=$residueMinue;//获得秒值
				}else{
					$minute=$intMinue;//获得分钟值
					$residueMinue=$residueHour%$unitMinute;
					$second=$residueMinue;//获得秒值
				}
				
			}
		}else{
			$day=$intDay;   //获得天值
			$residueDay=$secondCount%$unitDay;
			$intHour=$residueDay/$unitHour;

			if($intHour==0){
				$residueHour=$residueDay;
				$intMinue=$residueHour/$unitMinute;
				if($intMinue==0){
					$residueMinue=$residueHour;
					$second=$residueMinue;
				}else{
					$minute=$intMinue;//获得分钟
					$residueMinue=$residueHour%$unitMinute;
					$second=$residueMinue;
				}
			}else{
				$hour=$intHour;//获得小时值
				$residueHour=$residueDay%$unitHour;//
				$intMinue=$residueHour/$unitMinute;
				if($intMinue==0){
					$residueMinue=$residueHour;
					$second=$residueDay;//获得秒值
				}else{
					$minute=$intMinue;//获得分钟值
					$residueMinue=$residueHour%$unitMinute;
					$second=$residueMinue;//获得秒值
				}
				
			}
			
		}
		$day=(integer)$day;
		$hour=(integer)$hour;
		$minute=(integer)$minute;
		if($day!=0){
			$str=$str.$day."天,";
		}
		if($hour!=0){
			$str=$str.$hour."小时,";
		}
		if($minute!=0){
			$str=$str.$minute."分钟,";
		}
		if($second!=0){
			$str=$str.$second."秒,";
		}
		if($day==0 && $hour==0 && $minute==0 && $second==0){
			$str=$str.$second."秒,";
		}
		$length=strlen($str);
		$str=substr($str, 0,$length-1);
		return $str;
		
	}
	
	
}
?>